/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author SONY
 */
public class DBUtil {
    private final String server = "SONY-VAIO";
    private final int port = 1433;
    private final String database = "EmployeeTransferManagement";
    private final String user = "sa";
    private final String password = "123456";
    
    Connection conn = null;
    
//
    public String getURLConnection(){
        return  "jdbc:sqlserver://" + server + ":" + port +
                ";databaseName=" + database +
                ";User=" + user +
                ";Password=" + password;
    }
    public Connection connect(){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Khong tim thay Drive");
        }
        try {
            conn = DriverManager.getConnection(getURLConnection());
        } catch (SQLException ex) {
            System.out.println("Khong the ket noi voi CSDL");
            ex.getMessage();
        }
        return conn;
    }
    
    public int excuteUpdate(String SQL) throws Exception
    {
        Statement st = conn.createStatement();
        int kq = st.executeUpdate(SQL);
        return kq;
    }
    
    public ResultSet executeQuery(String SQL) throws SQLException
    {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);
        return rs;
    }
    
    public ResultSet executeSQL(String querySQL) throws SQLException{
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = stmt.executeQuery(querySQL);
        return rs;
    }
}
